/*
 * @(#)CommonCodeAction.java $version 2013-3-1
 * 
 * Copyright 2013 NADO Group. All rights Reserved.
 * NADO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nado.iims2.community.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nado.extensions.util.DataMap;
import com.nado.iims2.community.bo.CommonCodeBO;
import com.nado.iims2.community.model.CategoryModel;
import com.nado.iims2.community.model.CodeModel;

/**
 * The Class CommonCodeAction.
 * 
 * @author nado
 */
@Controller
public class CommonCodeAction {
	private static Log log = LogFactory.getLog(CommonCodeAction.class);

	private final static String CATEGORY_LIST = "/manage/categoryList";
	private final static String CATEGORY_FORM = "/manage/categoryForm";
	private final static String CODE_LIST = "/manage/codeList";
	private final static String CODE_FORM = "/manage/codeForm";
	
	private final static String PATH_CATEGORY = "category";
	private final static String PATH_CODE = "code";
	
	@Autowired
	private CommonCodeBO commonCodeBO = null;
	
	@RequestMapping(value = "/manage/{path}", method = RequestMethod.GET)
	public String list(@PathVariable("path") String path, HttpServletRequest request, Model model) {
		log.debug("@@@ /manage/{" + path + "}...");
		
		if(path.equals(PATH_CATEGORY)) {
			return listCategory(model);
		}
		
		if(path.equals(PATH_CODE)) {
			String categoryCd = request.getParameter("catCd");
			return listCode(categoryCd, model);
		}
		
		return null;
	}
	
	@RequestMapping(value = "/manage/{path}/form", method = RequestMethod.GET)
	public String form(@PathVariable("path") String path, HttpServletRequest request, Model model) {
		log.debug("@@@ /manage/{" + path + "}/form...");
		
		String categoryCd = StringUtils.defaultIfEmpty(request.getParameter("catCd"), "");
		String commonCd = StringUtils.defaultIfEmpty(request.getParameter("comCd"), "");
		
		if(path.equals(PATH_CATEGORY)) {
			return formCategory(categoryCd, model);
		}
		
		if(path.equals(PATH_CODE)) {
			return formCode(categoryCd, commonCd, model);
		}
		
		return null;
	}
	
	@RequestMapping(value = "/manage/{path}/check", method = RequestMethod.GET)
	public @ResponseBody Map<String, ? extends Object> checkPrimaryKey(
			@PathVariable("path") String path, HttpServletRequest request) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String categoryCd = request.getParameter("catCd");
		String commonCd = request.getParameter("comCd");
		int statusCode = 0;
		
		if(path.equals(PATH_CATEGORY)) {
			statusCode = commonCodeBO.getCommonCategoryDetail(categoryCd) == null ? 0 : 1;
		} 
		
		if (path.equals(PATH_CODE)) {
			statusCode = commonCodeBO.getCommonCodeDetail(categoryCd, commonCd) == null ? 0 : 1;
		}
		
		jsonMap.put("statusCode", statusCode);
		return jsonMap;
	}
	
	@RequestMapping(value = "/manage/category/write", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> registCategory(
			@ModelAttribute CategoryModel model) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		int statusCode = 0;
		
		model.setRegistEmpNo("ADMIN");
		
		statusCode = commonCodeBO.writeCommonCategory(model);
		
		jsonMap.put("statusCode", statusCode);
		return jsonMap;
	}

	@RequestMapping(value = "/manage/code/write", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> registCode(
			@ModelAttribute CodeModel model) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		int statusCode = 0;
		
		model.setRegistEmpNo("ADMIN");
		
		statusCode = commonCodeBO.writeCommonCode(model);
		
		jsonMap.put("categoryCd", model.getCategoryModel().getCategoryCd());
		jsonMap.put("statusCode", statusCode);
		return jsonMap;
	}

	private String listCategory(Model model) {
		List<DataMap> categoryList = commonCodeBO.getCommonCategoryList();
		
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("categoryCount", categoryList.size());
		
		return CATEGORY_LIST;
	}
	
	private String listCode(String categoryCd, Model model) {
		List<DataMap> codeList = commonCodeBO.getCommonCodeList(categoryCd);
		
		model.addAttribute("codeList", codeList);
		model.addAttribute("codeCount", codeList.size());
		model.addAttribute("categoryCd", categoryCd);
		return CODE_LIST;
	}
	
	private String formCategory(String categoryCd, Model model) {
		boolean isNew = true;
		CategoryModel categoryModel = null;
		
		if(!"".equals(categoryCd)) {
			categoryModel = commonCodeBO.getCommonCategoryDetail(categoryCd);
			isNew = false;
		} else {
			categoryModel = new CategoryModel();
		}
		
		model.addAttribute("isNew", isNew);
		model.addAttribute(categoryModel);
		return CATEGORY_FORM;
	}
	
	private String formCode(String categoryCd, String commonCd, Model model) {
		boolean isNew = true;
		CodeModel codeModel = null;
		
		if(!"".equals(commonCd)) {
			log.debug("true");
			codeModel = commonCodeBO.getCommonCodeDetail(categoryCd, commonCd);
			isNew = false;
		} else {
			log.debug("false");
			CategoryModel categoryModel = new CategoryModel();
			categoryModel.setCategoryCd(categoryCd);
			
			codeModel = new CodeModel();
			codeModel.setCategoryModel(categoryModel);
		}
		
		model.addAttribute("isNew", isNew);
		model.addAttribute(codeModel);
		return CODE_FORM;
	}

}
